var i = 1;
var imageArr=new Array();

function add_file(file, radio_value) {
	
	var j = i - 1;

	var file_exists = 0;

	for (x = 0; x < j; x++) {
		if (imageArr[x] == file) {
			alert('This file has already been added to the Upload List');
			file_exists = 1;
			break;
		}
		else{
			imageArr[j] = file;
		}
	}

	if (file_exists == 0) {

		// For Internet Explorer
		try {
			//input radio butron
			el = document.createElement('<input type="radio" name="image_title" value="'+(radio_value+i)+'" />');
			//input delete button
			del_el = document.createElement('< <INPUT type="button" value="Delete" onclick="javascript:deleteRow('+file+', '+j+');" />');
			
			file_el = document.createElement('<input type="file" name="userfile[]" id="file_' + i + '" size="30" onChange="javascript:add_file(this.value, '+(radio_value+i+1)+');">');
		}
		// For other browsers
		catch (e) {
			//create the new radiobutton
	        el = document.createElement('input');
			el.setAttribute('type', 'radio');
			el.setAttribute('name', 'image_title');    
			el.setAttribute('value', (radio_value+i));   
			
			//create the new delete button
	        del_el = document.createElement('input');
	        del_el.setAttribute('type', 'button');
			del_el.setAttribute('value', 'Delete');   			
			del_el.setAttribute('onclick', 'javascript:deleteRow("'+file+'", "'+j+'")');	 
			
			//replace file input
			file_el = document.createElement('input');
			file_el.setAttribute('type', 'file');
			file_el.setAttribute('name', 'userfile[]');
			file_el.setAttribute('id', 'file_' + i);
			file_el.setAttribute('size', '30');
			file_el.setAttribute('onChange', 'javascript:add_file(this.value, '+(radio_value)+');');			
		}
		
		document.getElementById('file_' + j).style.display = 'none';

    	var tbody = document.getElementById("imageTable").getElementsByTagName("TBODY")[0];

        var row = document.createElement("TR");
        var td1 = document.createElement("TD");
        td1.appendChild(document.createTextNode(file));
        var td2 = document.createElement("TD");
        var td3 = document.createElement("TD");

		if (document.getElementById('imageTable').style.display == 'none') {
			//alert('setting to block');
			document.getElementById('imageTable').style.display = 'block';
		}

        td2.appendChild(el);
        td3.appendChild(del_el);
        row.appendChild(td1);
        row.appendChild(td2);
        row.appendChild(td3);
        tbody.appendChild(row);
        
        document.getElementById('files_div').appendChild(file_el);

		i++;
	}

}

function deleteRow(file_el, row_to_delete) {

    try {
    var table = document.getElementById("imageTable");

    	row_to_delete++;
        table.deleteRow(row_to_delete);
     
        //remove from list of files to upload
        var files_div = document.getElementById('files_div');
        var boxpara = document.getElementById("file_"+row_to_delete);
        files_div.removeChild(boxpara);

    }
    catch(e) {
        alert(e);
    }
}

function remove_file() {
	var box = document.getElementById('file_list');

	if (box.selectedIndex != -1) {
		var value = box.options[box.selectedIndex].value;
		var child = document.getElementById(value);
	
		box.options[box.selectedIndex] = null;
		document.getElementById('files_div').removeChild(child);
	
		if (box.length == 0) {
			document.getElementById('list_div').style.display = 'none';
		}
	}
	else {
		alert('You must first select a file from the list.');
	}
}

function do_submit() {
// Uncomment this block for the real onSubmit code

	var box = document.getElementById('file_list');
	var max_files = 5;
	
	if (box.length <= max_files) {	
		var child = document.getElementById('file_' + (i - 1));

		div = document.getElementById('files_div');
		div.removeChild(child);
		div.style.display = 'none';

		return true;
	}
	else
	{
		alert('You have more files listed than the maximum allowed.\nPlease limit your upload files to no more than '+max_files+' at a time.');
		return false;
	}

// Just for test page
	alert('Files uploaded successfully');
	return false;
}